<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="com.lero.model.Admin" %>
<%@ page import="com.lero.model.DormManager" %>
<%@ page import="com.lero.model.Student" %>
<%
	if(request.getAttribute("user")==null){
		String userName=null;
		String password=null;
		String userType=null;
		String remember=null;
		
		Cookie[] cookies=request.getCookies();
		for(int i=0;cookies!=null && i<cookies.length;i++){
			if(cookies[i].getName().equals("dormuser")){
				userName=cookies[i].getValue().split("-")[0];
				password=cookies[i].getValue().split("-")[1];
				userType=cookies[i].getValue().split("-")[2];
				remember=cookies[i].getValue().split("-")[3];
			}
		}
		
		if(userName==null){
			userName="";
		}
		
		if(password==null){
			password="";
		}
		
		if(userType==null){
			userType="";
		} else if("admin".equals(userType)){
			pageContext.setAttribute("user", new Admin(userName,password));
			pageContext.setAttribute("userType", 1);
		} else if("dormManager".equals(userType)) {
			pageContext.setAttribute("user", new DormManager(userName,password));
			pageContext.setAttribute("userType", 2);
		} else if("student".equals(userType)) {
			pageContext.setAttribute("user", new Student(userName,password));
			pageContext.setAttribute("userType", 3);
		}
		
		if("yes".equals(remember)) {
			pageContext.setAttribute("remember", 1);
		}
		
	}
%>
<html lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>宿舍管理系统登录</title>
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/bootstrap/css/bootstrap-responsive.css" rel="stylesheet">
<script src="${pageContext.request.contextPath}/bootstrap/js/jQuery.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap/js/bootstrap.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap/js/Particleground.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap/js/Base64.js"></script>
<link rel="icon" href="${pageContext.request.contextPath}/images/logo.png" sizes="42x36">
<script type="text/javascript">
	
	function checkForm() {
		var userName = document.getElementById("userName").value;
		var password = document.getElementById("password").value;
		var userTypes = document.getElementsByName("userType");
		var userType = null;
		for(var i=0;i<userTypes.length;i++) {
			if(userTypes[i].checked) {
				userType=userTypes[i].value;
				break;
			}
		}
		if (userName == null || userName == "") {
			document.getElementById("error").innerHTML = "用户名不能为空";
			return false;
		}
		if (password == null || password == "") {
			document.getElementById("error").innerHTML = "密码不能为空";
			return false;
		}
		if (userType == null || userType == "") {
			document.getElementById("error").innerHTML = "请选择用户类型";
			return false;
		}
		return true;
	}
</script>

<style type="text/css">
	  body{height:100%;background:#88add9;overflow:hidden;}
		canvas{z-index:-1;position:absolute;}
		.form_login{
			width: 330px;
			height: auto;
			padding: 20px;
			margin: 0 auto;
			box-shadow:0 -15px 30px #0005;
			border-radius:5px;
			position:relative;
			top:150px;
		}
		.login-btn{
			border-radius: 5px;
		    background: transparent;
		    padding: 5px 25px;
		    border: 2px solid #4FA1D9;
		    color: #fff;
		    text-transform: uppercase;
		    font-size: 16px;
		}
		.login-btn:hover{
		    background: #4FA1D9;
		    color: #fff;
		}
</style>

</head>
<body>
<dl class="admin_login">
 	  <form name="myForm"  action="login" class="form_login" method="post" onsubmit="return checkForm()"style="color: #fff;">
        <h2 class="form-signin-heading" style="text-align: center;">宿舍管理系统</h2>
        <input id="userName" name="userName" value="${user.userName }" type="text" class="input-block-level" placeholder="账号">
        <input id="password" name="password" value="${user.password }" type="password" class="input-block-level" placeholder="密码" >
        <label class="radio inline">
      	  	<input id="admin" type="radio" name="userType" value="admin"  checked/> 系统管理员
		</label>
		<label class="radio inline">
			<input id="dormManager" type="radio" name="userType" value="dormManager" ${userType==2?'checked':''} /> 宿舍管理员
		</label>
		<label class="radio inline">
			<input id="student" type="radio" name="userType" value="student"  ${userType==3?'checked':''}/> 学生
		</label>
        <label class="checkbox">
          <input id="remember" name="remember" type="checkbox" value="remember-me" ${remember==1?'checked':''}>记住我 &nbsp;&nbsp;&nbsp;&nbsp; <font id="error" color="red">${error }</font>  
        </label>
        <button class="login-btn" id="login" type="button">登录</button>
        &nbsp;&nbsp;
        <button class="login-btn" id="cz" type="button" >重置</button>
        &nbsp;&nbsp;
        <button class="login-btn" id="face-login" type="button" >人脸登录</button>

		<p align="center" style="padding-top: 15px;">版权所有  湖南文理学院计电学院软工第一小组</p>
      </form>
</dl>

</body>
<script type="text/javascript">
$(document).ready(function() {
  if(GetQueryString("p") != null){
  	selectRadio("userType",window.localStorage.getItem("role"));
  	$("#userName").val(GetQueryString("a"));
  	$("#password").val(Secret_Key(GetQueryString("p"),"123123",'decryption'));
  	window.localStorage.setItem("username",GetQueryString("a"));
  	window.localStorage.setItem("pwd",GetQueryString("p"));
  	$(".form_login").submit();
  }
  //粒子背景特效
  $('body').particleground({
    dotColor: '#9dd3fa',
    lineColor: '#9dd3fa'
  });
  $("#face-login").on("click",function(){
  	//保存选择的标签后再跳转到人脸识别
  	var role = $('input:radio[name="userType"]:checked').val();
  	window.localStorage.setItem("role",role);
  	$(location).attr("href","https://www.cxlwt.cn/shixunDemo/index.html?type=login&role="+role);
  })
  $("#login").on("click",function(){
  	var role = $('input:radio[name="userType"]:checked').val();
  	window.localStorage.setItem("role",role);
  	window.localStorage.setItem("username",$("#userName").val());
  	window.localStorage.setItem("pwd",Secret_Key($("#password").val(),"123123",'encryption'));
  	$(".form_login").submit();
  })
  $("#cz").on("click",function(){
  	$("#userName").val("");
  	$("#password").val("");
  })
})
function selectRadio(name,id){
    $("input[name="+name+"]").each(function(){
        $(this).prop("checked",false);
    });
    $("#"+id).prop("checked",true);
}
function GetQueryString(name)

  {

       var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");

       var r = window.location.search.substr(1).match(reg);

       if(r!=null)return  unescape(r[2]); return null;

  }
</script>
</html>